package pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class InternalPage extends AnyPage {

	public InternalPage(PageManager pages) {
		super(pages);
	}

  public InternalPage ensurePageLoaded() {
    super.ensurePageLoaded();
    wait.until(presenceOfElementLocated(By.className("languages")));
    return this;
  }

  @FindBy(css = "nav a[href $= '?go=profile']")
  private WebElement userProfileLink;

  @FindBy(css = "nav a[href $= '?go=users']")
  private WebElement userManagementLink;

  //@FindBy(css = "nav a[onclick $= '?logout']")
  @FindBy(linkText = "Log out")
  private WebElement logoutLink;

  @FindBy (css="img[alt=\"Add movie\"]")
  private WebElement addFilmButton;

  @FindBy (id="q")
  private WebElement searchInput;

  @FindBy (css="div#content")
  private WebElement searchResult;

  @FindBy (css="div.title")
  private WebElement movieTitle;

  public String getRandomMovieTitle() {
		return movieTitle.getText();
  }

  public FilmViewPage clickMovieTitle(String movieTitle){
	  searchMovieByKeyword(movieTitle);
	  this.movieTitle.click();
	  return pages.filmViewPage;
  }

  public UserProfilePage clickUserProfilePage() {
    userProfileLink.click();
    return pages.userProfilePage;
  }

  public UserManagementPage clickUserManagementLink() {
    userManagementLink.click();
    return pages.userManagementPage;
  }

  public FilmEditPage clickAddFilmButton(){
	  addFilmButton.click();
	  return pages.filmEditPage;
  }

  public LoginPage clickLogoutLink() {
    logoutLink.click();
    wait.until(alertIsPresent()).accept();
    return pages.loginPage;
  }

public InternalPage findAllMovies() {
	searchInput.clear();
	searchInput.sendKeys(Keys.RETURN);
	return this;
}

public InternalPage searchMovieByKeyword(String movieKeyword){
	searchInput.clear();
	searchInput.sendKeys(movieKeyword + Keys.RETURN);
	try{ Thread.sleep(1000);}catch (InterruptedException ie) {}
	return this;
}

public boolean noMovies(){
	if (searchResult.getText().contains("No movies where found."))
		return true;
	return false;
}



}
